package com.i.categoryservice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.i.categoryservice.service.ICategoryService;
import com.i.piccommon.constant.ApiResponse;
import com.i.piccommon.domain.Category;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lewis
 * @type class
 * @description
 * @since 2024/10/27
 */
@RestController
@RequiredArgsConstructor
public class CategoryController {
     private final ICategoryService service;
     @GetMapping
     public ApiResponse categorys(){
         List<Category> list = service.list();
         if(list != null){
             return ApiResponse.ok(list);
         }
         return ApiResponse.fail();
        }
     @GetMapping("/{cid}")
     public ApiResponse categoryOne(@PathVariable Integer cid) {
         Category category = service.getById(cid);
         return category != null ? ApiResponse.ok(category) : ApiResponse.fail();
     }
    @PostMapping
    public ApiResponse addCategory(@RequestBody Category category){
        boolean save = service.save(category);
        if(save) {
            return ApiResponse.ok(save);
        }
        return ApiResponse.fail();
    }
    @PutMapping
    public ApiResponse modifyCategory(@RequestBody Category category){
        boolean b = service.updateById(category);
        if(b){
            return ApiResponse.ok(true);
        }
        return ApiResponse.fail();
    }
    @DeleteMapping("/{cid}")
    public ApiResponse deleteCategory(@PathVariable(value = "cid") Integer categoryId){
        boolean b = service.removeById(categoryId);
        if(b){
            return ApiResponse.ok(b);
        }
        return ApiResponse.fail();
    }
    @PostMapping("/page")
    public ApiResponse<IPage<Category>> searchPhotoByCategory(@RequestBody Category categoryVO){
        IPage<Category> categories = service.searchPhotoByCategory(categoryVO);
        return ApiResponse.ok(categories);
    }
}
